home *** CD-ROM | disk | FTP | other *** search
/ Best of www.BestZips.com (Collector's Edition) / Best of WWW.BESTZIPS.COM Collector's Edition (JCSM Shareware) (JCS Marketing).ISO / gamebord / chsclk20.zip / CHESSCLK.CPP < prev    next >
C/C++ Source or Header  |  1996-10-23  |  13KB  |  476 lines

  1. /****************************** Chess clock program ************************
  2.       FULL-FEATURED CHESS CLOCK
  3.       Large digital displays of clocks
  4.                Move counter
  5.                White and Black clocks separately settable
  6.                Times from 1 minute to 9 hours
  7.                Low-time warning (optional)
  8.                Flashing "tick" indicator (optional)
  9.       Flag for active player's clock
  10.       Audible player changeover indicator
  11.       Pause control
  12.       NEW: Optional "overtime" tracking in Word Game Mode
  13. ****************************************************************************/
  14. // M\Cooper, PO Box 237, St. David, AZ 85630-0237
  15. // E-mail: thegrendel@theriver.com
  16. // Web:  http://personal.riverusers.com/~thegrendel/
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <conio.h>
  22. #include <stdlib.h>
  23. #include <graphics.h>
  24. #include <dos.h>
  25. #include "oscr.hpp"
  26. #include "chessclk.hpp"
  27.  
  28. #define XXPOS 60
  29. #define YYPOS 37
  30. // Base coordinates for time display
  31.  
  32. #define PX   197
  33. #define PY   130
  34. // Coordinates for "pause" message
  35.  
  36. #define XWIDTH 580
  37. #define YHEIGHT 25
  38. #define XXMARGIN 50
  39. #define LXMARGIN 26
  40. // *Whose_clock_is_running* flag coordinates
  41.  
  42. #define DEFAULT_TIME 2
  43. #define DEFAULT_TEST() if( hrs==0 && min == 0 ) hrs = DEFAULT_TIME
  44.  
  45. //Word Game Mode Indicator Message
  46. #define X_m 260
  47. #define Y_m 30
  48. #define wgm_color MAGENTA
  49. char wgm_message [] = "Word Game Mode";
  50.  
  51.    FLAG ScrabFLAG = OFF;
  52.    int wpsecs = 0,
  53.        bpsecs = 0;
  54.      
  55.  
  56. void main( int argc, char **argv )
  57. {
  58.       if( argc > 1 && ( *( *(argv+1) ) == 's' || *( *(argv+1) ) == 'S' ) )
  59.           ScrabFLAG = ON; //Wanna play wordgames?
  60.  
  61.    randomize();
  62.    opening_screen();
  63.    play();
  64. }
  65.  
  66. void CountdownTimer::clock_on()
  67. {
  68.    time_t prev_sec;
  69.    int ch;
  70.  
  71.    toFLAG = OFF;
  72.    pause_t = 0;
  73.  
  74.       if( p == WHITE_ )
  75.      text_color = LIGHTBLUE;
  76.       else
  77.      text_color = DARKGRAY;
  78.  
  79.       setcolor( text_color );
  80.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  81.       settextjustify( LEFT_TEXT, TOP_TEXT );
  82.  
  83.      ShowFlag();
  84.  
  85.  
  86.       /**************INITIALIZE**********************/
  87.       startn_t = time ( NULL );  //Click on stopwatch.
  88.       /**********************************************/
  89.  
  90.       gotoxy( XXPOS * p + 1, YYPOS );
  91.       outtextxy( NAME_POS * p, 100, player [p] );
  92.       display_time();  //Otherwise initial time not displayed...
  93.  
  94.       while( !( ch = kbhit() ) )
  95.          {
  96. /**************************(1)***************************/
  97.  
  98.      prev_sec = seconds;
  99.      if( running_flag ) //Is this 2nd or later lap?
  100.         interval_t = time( NULL ) - startn_t;
  101.      else
  102.         interval_t = time( NULL ) - start_t;
  103.  
  104. /**********************XXXXX**********PAUSE-TIME**********************/
  105.     if( !overtime_flag )
  106.       running_t = total_seconds - interval_t - pause_t;
  107.     else //if overtime
  108.       {
  109.       running_t =  interval_t - total_seconds - pause_t;
  110.       text_color = LIGHTRED;
  111.       }
  112. //                                              ^^^^^^^
  113.  
  114.     if( overtime_flag && running_flag )
  115.       running_t = interval_t + total_seconds - pause_t;
  116. //            
  117.      convert( running_t );
  118.  
  119.      if( seconds - prev_sec )
  120.         {
  121.         display_time();
  122.  
  123.         if( !seconds )
  124.            if( minutes == warning )
  125.           if( !hours )
  126.              if( time_warning_flag )
  127.             blatt();
  128.  
  129.         if( visual_ticking_flag ) // Show blinking box ticks?
  130.            {
  131.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  132.            setcolor ( random ( COLORS ) );
  133.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  134.            bar( X_C - RADIUS, Y_C - RADIUS,
  135.             X_C + RADIUS, Y_C + RADIUS );
  136.            }
  137.         }
  138.  
  139.      if( timeout() && !ScrabFLAG )
  140.         exit_();
  141.   
  142. /******************(2)******************/
  143.   if( timeout() && ScrabFLAG && !toFLAG )
  144.      {
  145.      overtime_flag = ON;
  146.  
  147.      startn_t = time( NULL );
  148.      toFLAG = ON;
  149.  
  150.      if( running_flag)
  151.         total_seconds = 0; /*********reset******/
  152.  
  153.      erase_numbers();
  154.      }
  155.  
  156.      }
  157.  
  158.       total_seconds = hours * 3600 + minutes * 60 + seconds;
  159.       running_flag = ON;  // Remember that clock was already running.
  160.  
  161.       
  162.       ch = getch(); //Retrieve keypress.
  163.       if( ch == ESC )
  164.           {
  165.            if( overtime_flag && p == WHITE_ )
  166.                wpsecs = total_seconds; 
  167.            if( overtime_flag && p == BLACK_ )
  168.                bpsecs = total_seconds;
  169.  
  170.            exit__(); //Quit.
  171.           }
  172.            
  173.  
  174.       if( ch == 'p' || ch == 'P' )
  175.          {
  176.            if( overtime_flag && p == WHITE_ )
  177.                wpsecs = total_seconds; 
  178.            if( overtime_flag && p == BLACK_ )
  179.                bpsecs = total_seconds;
  180.  
  181.          pause();
  182.           }
  183.  
  184.  
  185.            if( overtime_flag && p == WHITE_ )
  186.                wpsecs = total_seconds; 
  187.            if( overtime_flag && p == BLACK_ )
  188.                bpsecs = total_seconds;
  189.  
  190.  
  191.       return;
  192.  
  193. }
  194.  
  195. void CountdownTimer::display_moves()
  196. {
  197.    char buf[ 5 ];
  198.    static char ebuf[ 5 ];
  199.  
  200.       if( moves > 1 )
  201.      {
  202.      setcolor( WHITE );
  203.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  204.      settextjustify( CENTER_TEXT, TOP_TEXT );
  205.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  206.      }
  207.  
  208.       sprintf( buf, "%003d", moves );
  209.       sprintf( ebuf, buf );
  210.       setcolor( GREEN );
  211.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  212.       settextjustify( CENTER_TEXT, TOP_TEXT );
  213.       outtextxy( MOVES_X, MOVES_Y, buf );
  214.       
  215.       return;
  216. }
  217.  
  218. void graphics_setup( int background_color )
  219. {
  220.    int grdriver = VGA,
  221.        grmode = VGAHI;
  222.  
  223.        registerfarbgidriver( EGAVGA_driver_far );
  224.        registerfarbgifont( gothic_font_far );
  225.        registerfarbgifont( triplex_font_far );
  226.        initgraph( &grdriver, &grmode, "" );
  227.        setbkcolor( background_color );
  228.  
  229. // Note: Borland BGI graphics drivers are completely
  230. //       incompatible with Microsloth Wimpdows.
  231. //       ============
  232.  
  233. }
  234.  
  235.  
  236. /*************************FINAL STATS?**************************/
  237. void exit__()
  238. {
  239.       closegraph();
  240.  
  241.       if( ScrabFLAG )
  242.          {
  243.            printf( "White has %d minute(s) and %d second(s) of overtime.\n",
  244.                     wpsecs/60, wpsecs%60 );
  245.            printf( "Penalty points for White = %d.\n\n",
  246.                      wpsecs/6 );
  247.  
  248.            printf( "Black has %d minute(s) and %d second(s) of overtime.\n",
  249.                     bpsecs/60, bpsecs%60 );
  250.            printf( "Penalty points for Black = %d.\n\n",
  251.                      bpsecs/6 );
  252.          }
  253.      
  254.       exit( QUIT );
  255. }
  256.  
  257.     /***************Routine to erase old numbers*************/
  258. void CountdownTimer::erase_numbers()
  259. {
  260.       setcolor ( WHITE ); 
  261.  
  262.       if( seconds == 59 || ( seconds == 0 && ScrabFLAG ) )
  263.      outtextxy( p * BLK_TIME, Y_TIMEPOS, line_clear );
  264.       else
  265.      if( seconds == 9 || seconds == 19 || seconds == 29
  266.          || seconds == 39 || seconds == 49
  267.       || ( seconds == 10 && ScrabFLAG )
  268.       || ( seconds == 20 && ScrabFLAG )
  269.       || ( seconds == 30 && ScrabFLAG )
  270.       || ( seconds == 40 && ScrabFLAG )
  271.       || ( seconds == 50 && ScrabFLAG )  )
  272.         outtextxy( p * BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  273.                line_clear + 6 );
  274.       else
  275.          outtextxy( p * BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  276.             line_clear + 7 ); 
  277.  
  278.      return;
  279.  
  280. }
  281.  
  282.  
  283. void play()
  284. {
  285.    int hrs,
  286.        min;
  287.    char inputstr[ MAXLEN ],
  288.     inp;
  289.  
  290.       clrscr();
  291.  
  292.       textcolor ( LIGHTCYAN );
  293.       cprintf( "\n                              WHITE hours: " );
  294.       gets( inputstr );
  295.       hrs = atoi( inputstr );
  296.       cprintf( "                              WHITE minutes: " );
  297.       gets( inputstr );
  298.       min = atoi( inputstr );
  299.       DEFAULT_TEST();
  300.       CountdownTimer t1( hrs, min, WHITE_ );
  301.  
  302.       textcolor( RED );
  303.       cprintf( "\n                              BLACK hours: " );
  304.       gets( inputstr );
  305.       hrs = atoi( inputstr );
  306.       cprintf( "                              BLACK minutes: " );
  307.       gets( inputstr );
  308.       min = atoi( inputstr );
  309.       DEFAULT_TEST();
  310.       CountdownTimer t2( hrs, min, BLACK_ );
  311.  
  312.       textcolor( YELLOW );
  313.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  314.       inp = getche();
  315.       if( inp == 'y' || inp == 'Y' )
  316.      t1.visual_ticking_flag = t2.visual_ticking_flag = ON;
  317.       else
  318.      t1.visual_ticking_flag = t2.visual_ticking_flag = OFF;
  319.  
  320.       cprintf( "\n                                                      Enable time warning? " );
  321.       inp = getche();
  322.       if( inp == 'y' || inp == 'Y' )
  323.      {
  324.      t1.time_warning_flag = t2.time_warning_flag = ON;
  325.      cprintf( "                                                          At how many minutes? " );
  326.      gets( inputstr );
  327.      t1.warning = t2.warning = atoi( inputstr );
  328.      }
  329.       else
  330.      t1.time_warning_flag = t2.time_warning_flag = OFF;
  331.  
  332.       textcolor( GREEN | BLINK );
  333.       _setcursortype( _NOCURSOR );
  334.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  335.       cprintf( "                             PRESS A KEY TO BEGIN" );
  336.       while ( !getch() );  //Wait for keypress - to begin play.
  337.       beep1();  //BEEP
  338.  
  339.       graphics_setup( WHITE );
  340.  
  341.       /***************************888DISPLAY SCRAB MSG*********************/
  342.       if( ScrabFLAG )
  343.           t1.Write_Message( X_m, Y_m, wgm_message , MAGENTA );
  344.      /*********************************************************************/
  345.  
  346.  
  347.  
  348.  
  349.  
  350.       t1.initialize_clock();
  351.       t1.moves++;
  352.       t1.display_moves();
  353.       t1.clock_on();
  354.       beep1();
  355.  
  356.       t2.initialize_clock();
  357.       t2.clock_on();
  358.       beep2();
  359.  
  360.       while ( PLAY )
  361.      {
  362.      t1.moves++;  
  363.      t1.display_moves();
  364.      t1.clock_on();
  365.       beep1();
  366.      t2.clock_on(); 
  367.       beep2();
  368.      }
  369.   
  370.  
  371. } // End play()
  372.  
  373.  
  374. void opening_screen()
  375. {
  376.    char topline[] = "Chess Clock",
  377.     by_line[] = "by",
  378.     name_line[] = "M\\Cooper",
  379. email_line[] = "thegrendel@theriver.com",
  380.     endline[] = "PRESS A KEY";
  381.  
  382.       graphics_setup( CYAN );
  383.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  384.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  385.       setcolor( LIGHTRED );
  386.       outtextxy( TOPX, TOPY, topline );
  387.  
  388.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  389.       setcolor( BLUE );
  390.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  391.  
  392.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  393.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  394.  
  395.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  396.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  397.       pieslice( PIE2_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  398.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  399.       circle( PIE2_X, PIE_Y, CIRC_RAD );
  400.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  401.       line( LINE2_X, LINE_Y1, LINE2_X, LINE2_Y2 );
  402.       setfillstyle( PIE_PATTERN, WHITE );
  403.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  404.       bar( B2_LEFT, B2_TOP, B2_RIGHT, B_BOTTOM );
  405.  
  406.  
  407.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  408.       setcolor( BLUE );
  409.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  410.  
  411.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE ); 
  412.       setcolor( BROWN );
  413.       outtextxy( EMLINE_X, EMLINE_Y, email_line ); 
  414.  
  415.       sleep( DELAY );
  416.  
  417.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  418.       setcolor( RED );
  419.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  420.  
  421.       getch();
  422.       closegraph();
  423.  
  424.       return;
  425. }
  426.  
  427. void CountdownTimer::ShowFlag()
  428. {
  429.      //Show flag over clock in use.
  430.      setfillstyle( INTERLEAVE_FILL, LIGHTRED );
  431.      bar( XXPOS * p + LXMARGIN, YYPOS,
  432.          XXPOS * p + XWIDTH - LXMARGIN, YYPOS + YHEIGHT );
  433.  
  434.      //Erase flag over clock *not* in use.
  435.      setfillstyle( SOLID_FILL, WHITE );
  436.      if( p == WHITE_ ) pp = BLACK_;
  437.      else pp = WHITE_;
  438.      bar( XXPOS * pp + LXMARGIN, YYPOS,
  439.          XXPOS * pp + XWIDTH - LXMARGIN, YYPOS + YHEIGHT );
  440.  
  441.      return;
  442. }
  443.  
  444. void CountdownTimer::pause()
  445. {
  446. //Pause clock when [p] key is pressed.
  447.  
  448.    time_t interim_time = 0;
  449.  
  450.  
  451.       beep2();   beep1();   //Sound for pause.
  452.  
  453.       pause_start_t = time( NULL );
  454.  
  455.       Write_Message( PX, PY, PauseMessage, LIGHTMAGENTA );  //Pause message.
  456.  
  457.       while( !getch() );   //Wait for keypress.
  458.  
  459.       interim_time = time( NULL );
  460.       pause_t = interim_time - pause_start_t;
  461.  
  462.       Write_Message( PX, PY, PauseMessage, WHITE );  //Erase message.
  463.  
  464.       return;
  465.  
  466. }
  467.  
  468. void CountdownTimer::Write_Message( int Px, int Py, char *Pmessage, int Pcolor )
  469. {
  470.       setcolor( Pcolor);
  471.       outtextxy( Px, Py, Pmessage );
  472.  
  473.       return;
  474.  
  475. }
  476.